<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * View file: index
 *
 * @author hoanguyen
 * @since 13/7/2013
 */
?>
<input type="hidden" name="base_url" id="base_url" value="<?php echo base_url() ?>" />

<div id="result"> </div>
<?php if (isset($del_noti)): ?>
    <h4 class="alert_success"><?php echo $del_noti ?></h4>
<?php endif; ?>
 
<article class="module width_full">
    <header>       
            <h3>
                Danh sách câu hỏi game   
                <img src="<?php echo base_url(); ?>/assets/css/_common/images/loadmorespinner.gif" id="imageloadmore" />
            </h3>                
    </header>

    <div id="question_note" style="float: left; padding: 7px 5px 0 5px;"> Bạn đang tìm kiếm câu hỏi có tag:  </div>
    <input id="question_list" type="hidden" name="tags" class="tagManager" />

    <div style="clear: both"></div>
    <table class="tablesorter" cellspacing="0">
        <thead>
            <tr>
                <th>Thứ tự</th>
                <th>Câu hỏi</th>
                <th>Chủ đề</th>
                <th>Ngày tạo</th>
                <th>Ngày cập nhật</th>                                               
                <th>Xử lý</th>      
            </tr>
        </thead>

        <tbody class="sortable" id="content" >
        </tbody>
    </table>

    <input id="end_timestamp" type="hidden" value="<?php echo time() ?>">    

    <div class="loadmore" id="loadmore">
        <a href="#" title="Loadmore" onclick="return loadmore(document.getElementById('question_list').value)"> Loadmore </a>
    </div>

    <div style=" margin: 5px; ">
        <input id="search_box" type="text" name="search_box" style="width: 98%; height: 1.5em;" 
               placeholder="Nhập từ khóa để tìm kiếm theo nickname" class="tagManager"
               onchange="return search_q(document.getElementById('search_box').value)"/>       
    </div>
</article> <!-- end of content manager article -->

</div>

<script>
    //Hiển thị danh sách các diễn viên khi truy cập trang index
    $(document).ready(function() {
        $('#question_note').hide();
        $('#imageloadmore').show();
        var timestamp= Math.round(new Date().getTime()/1000.0);
        $.ajax({
            url: $('#base_url').val() + "quizquestion/ajax_get_list_question",
            type: "POST",
            data: {end_timestamp: timestamp},
            dataType: 'json',
            success: function(msg) {
                insert_question_list(jQuery.parseJSON(msg.data), 'loadmore');
                $("#imageloadmore").hide();
            }
        });

        $('#search_box').typeahead({
            source: function (query, process) {
                return $.getJSON(
                $('#base_url').val() + 'question/ajax_search_question',
                { query: query },
                function (data) {
                    return process(data);
                });
            }				
        });
    });

    //Load more
    function loadmore(topic_id){
        $("#imageloadmore").show();
        end_timestamp = $('#end_timestamp').val();
        $.ajax({            
            url: $('#base_url').val() + "question/ajax_get_list_question",
            type: "POST",
            data: {end_timestamp: end_timestamp, topic_id: topic_id},
            success: function (msg) {
                insert_q_list(jQuery.parseJSON(msg.data), 'loadmore');
                $("#imageloadmore").hide();
            }
        });
    }        


    //Lọc danh sách movie theo các tag
    function search_tag(topic_id, topic){
        $(".myTag").remove();
        $("#imageloadmore").show();        

        if(topic != null){            
            jQuery("#question_list").tagsManager({
                prefilled: [
                    topic
                ],
                CapitalizeFirstLetter: true,
                preventSubmitOnEnter: true,
                typeahead: true,
                typeaheadAjaxSource: null,

                backspace: [8],
                blinkBGColor_1: '#FFFF9C',
                blinkBGColor_2: '#CDE69C',
                hiddenTagListName: 'hiddenTagList'
            });            
            
            $('#question_note').show();
        }else{                           
            $('#question_note').hide();
        }
        
        $('#question_list').val(topic);
        end_timestamp = $('#end_timestamp').val();
        //        end_timestamp = $('#end_timestamp').val();
        $.ajax({
            url: $('#base_url').val() + "quizquestion/ajax_get_list_question",
            type: "POST",
            data: {topic_id: topic_id},
            success: function (msg) {
                insert_question_list(jQuery.parseJSON(msg.data), 'search_tag');
                $("#imageloadmore").hide();
            }
        });
    }
        
    
    function insert_question_list(jsonArray, type){
        var i;
        var url = $('#base_url').val();        
        if(jsonArray != null){
            if(jsonArray.length == 0){
                data += '<tr>';
                data += '<td colspan="5">Không có dữ liệu</td>';
                data += '</tr>';
                $("#imageloadmore").hide();
            }
            else{                        
                data = '';
                for(i=0; i < jsonArray.length; i++){                    
                    question = jQuery.parseJSON(jsonArray[i])
                    
                    end_timestamp = question.update_timestamp;                               

                    data += '<tr>';

                    //STT
                    data += '<td>';                      
                    data += i + 1;                      
                    data += '</td>';                    

                    //Câu hỏi
                    data += '<td>' + question.question + '</td>';
                    
                    //Chủ đề
                    data += '<td>';
                    if(question.topicss != null)
                        for (j = 0; j < (question.topicss.length); j++) {                            
                            if(question.topicss[j].topics != '' )
                                data +=  '<a href="#" onclick="return search_tag('+"'"+question.topicss[j].id+"','"+question.topicss[j].topics+"'"+')"> ' +  question.topicss[j].topics + '</a>'                             
                        } 
                    data += '</td>';

                   //Ngày tạo
                    data += '<td>';                        
                    date = new Date(question.create_timestamp * 1000 );
                    datevalues = [date.getDate() + '/' + (date.getMonth()+1) + '/' + date.getFullYear()];
                    data += datevalues;                        
                    data += '</td>';

                    //Ngày Cập nhật
                    data += '<td>';                    
                    if(question.update_timestamp != null){
                        date = new Date(question.update_timestamp * 1000 );
                        datevalues = [date.getDate() + '/' + (date.getMonth()+1) + '/' + date.getFullYear()];
                        data += datevalues;  
                    }
                    data += '</td>';

                    //Xử lý
                    data += '<td>';
                    data += '<a href="' + url + 'quizquestion/form/' + question.id +'">Edit |</a>';
                    data += '<a href="' + url + 'quizquestion/delele/' + question.id + '"> Delete</a>';
                    data += '</td>';

                    data += '</tr>';
                }
                $('#end_timestamp').val(end_timestamp);

                if(type=='loadmore'){
                    $("#content").html($("#content").html()+data);
                }else{
                    $("#content").html(data);
                }
            }
        }
    }


</script>